<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>筛选条件</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <label class="layui-form-label">文档分类</label>
                        <div class="layui-input-inline">
                            <select id="cid">
                            </select>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-normal" lay-submit lay-filter="data-search-btn"><i class="layui-icon layui-icon-search"></i> 搜 索</button>
                            <button type="button" class="layui-btn layui-btn-warm" id="data-clear-btn">
                                <i class="layui-icon">&#xe639;</i> 清除
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 新增 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 批量删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>

<script type="text/html" id="cateNameTpl">
    {{d.category.cname}}
</script>

<script>
    layui.use(['form', 'table'], function () {
        let $ = layui.jquery,
            form = layui.form,
            table = layui.table;



        let tableList = table.render({
            elem: '#currentTableId',
            url: '/api/v1/document/search', //网络异步请求
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: '编号', sort: true},
                {field: 'title', title: '文档名称'},
                {field: 'summary', title: '文档摘要', sort: true},
                {field: 'uploader', title: '上传用户'},
                {field: 'createdTime', title: '上传时间'},
                {field: 'downloadTime', title: '下载时间', sort: true},
                {field: 'category',title: '所属分类', sort: true,templet: '#cateNameTpl'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            let cid = $('#cid').val();
            //执行搜索重新加载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    cid: cid
                }
            }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                layer.open({
                    title: '新增电子文档',
                    type: 2,
                    shadeClose: false,
                    shade:0.4,
                    maxmin: false,
                    area: ['580px', '640px'],
                    content: '../page/table/add.html',
                });

               /* $(window).on("resize", function () {
                    layer.full(index);
                });*/
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });
        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../page/table/edit.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('此操作将永久删除该文档, 是否继续?', {icon: 3, title:'确认提示'}, function (index) {
                    let id = obj.data.id;
                    let url = "/api/v1/document/delete/"+id;
                    $.post(url,function (res) {
                        if (res.code === 0){
                            tableList.reload();
                            layer.close(index);
                        }else{
                            layer.msg(res.data);
                        }
                    },'json');
                });
            }
        });
        //给分类下拉列表框动态赋值
        loadCategory();


        //清除筛选条件
        $('#data-clear-btn').on('click',function () {
            $('#cid').val(0);
            let cid = $('#cid').val();
            form.render('select');
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    cid: cid
                }
            }, 'data');
            return false;
        });

    });


    //文档分类select动态赋值
    function loadCategory() {
        let $ = layui.jquery;
        $.ajax({
            type: 'get',
            cache:false,
            url: '/api/v1/category/list',
            dataType:'json',
            success: function (res) {
                let data = res.data;
                $('#cid').html('<option value="0">全部</option>');
                $.each(data, function (key, item) {
                    let $option = $("<option>").val(item.id).text(item.cname);
                    $('#cid').append($option);
                });
                layui.form.render('select');
            }
        });
    }
</script>

</body>
</html>
